package com.sleepingrain;

public class SR_NDK_LIB {

	/*

	http://marakana.com/forums/android/examples/49.html
	
	recompile project
	=> <project>/bin/
	    javah -jni com.sleepingrain.SR_NDK_LIB
	=> move created header to <project>/jni
	=> <NDKRoot>
	    make APP=sr_ndk_lib

	*/

	static {
		System.loadLibrary("sr_ndk_lib");
	}
	
	// Native multiply Add, stride for interleaved data (in samples = channelCount..)
	static public native int multiplyAdd_Short(int[] destArray,short[] addendArray,int destOffset,int destStride,int addendOffset,int addendCount,int factor);

	// Native multiply Add Ex
	static public native int multiplyAddEx_Short(int[] destArray,short[] addendArray,int destOffset,int destStride,int addendOffset, int addendCount,float startFactor,float endFactor);

	// Native multiply Add With Border Fading
	static public native int multiplyAddFade_Int(int[] destArray,int[] addendArray,int destOffset,int addendOffset,int addendCount,int factor,int fadeSamples);

	// clamp2short: returns the int divisor, required to make everything fit
	static public native int clamp2short(int[] srcArray,int srcOffset,int srcCount,short[] destArray,int divisor,boolean updateDivisor);
	
	// move buffer back: moves data from offset back to beginning of the buffer and fills the gap with zeroes
	static public native int moveBuffer(int[] buffer,int bufferLength,int moveBackBySamples);

	static public native int fillBuffer(int[] buffer,int bufferOffset,int bufferCount,int value);

	// Native convert to short
	static public native int byte2short(byte[] srcArray,short[] destArray,int srcSize);

	// Native deinterlacing => 121212 => 111 222
	static public native int deinterlace(short[] srcArray,short[] destArray1,short[] destArray2, int srcSize);

	// resample: resamples input array to output array..
	// quality specifies the length of the windowed sinc filter
	static public native int resample(int[] srcArray,int srcLength,int[] destArray,int destLength,int quality);

	// Using speex resample
	// Init returns address of sampler state struct
	static public native long speex_resampler_init(int channelCount, long in_rate,long out_rate,int quality);
	// Process the given data and reset the state (no history)
	static public native int speex_resampler_process(long state,short[] inArray,long inLength, short[] outArray,long outLength);
	// Finalize the sampler
	static public native int speex_resampler_destroy(long state);
	
	// applies the filter in place - sampleCount is per channel!
	static public native int filter_lowShelf_Stereo(short[] samples,int sampleCount,long sampleRate,float freq,float gain);
	static public native int filter_highShelf_Stereo(short[] samples,int sampleCount,long sampleRate,float freq,float gain);
	
	/* BiQuad Filter:
	int type (0 LowShelfFilter, 1 HighShelfFilter, ..)
	float dbGain, 			gain of filter in decibel
    float freq,				center frequency
    long srate,				sampling rate
    float bandwidth			bandwidth in octaves */
	static public native int filter_biQuad_Stereo(short[] samples,int sampleCount,long sampleRate,int type,float freq,float bandwidth, float dbGain);
}
